{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.736634200Z",
     "start_time": "2024-05-14T06:22:28.654659600Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(12)\n",
    "x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.745069300Z",
     "start_time": "2024-05-14T06:22:29.737633900Z"
    }
   },
   "id": "bf502cd1063ccf2e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "number of elements"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a8d3b2a3bd8ce0f4"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "12"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.numel()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.753293900Z",
     "start_time": "2024-05-14T06:22:29.742068600Z"
    }
   },
   "id": "ec3947117d4c6b50"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0,  1,  2,  3],\n        [ 4,  5,  6,  7],\n        [ 8,  9, 10, 11]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.reshape(3, 4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.763746300Z",
     "start_time": "2024-05-14T06:22:29.752293700Z"
    }
   },
   "id": "5d006ba4683970d7"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0., 0., 0.],\n        [0., 0., 0.]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros((2, 3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.772025300Z",
     "start_time": "2024-05-14T06:22:29.761746300Z"
    }
   },
   "id": "8d61b9b5a2a33a63"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.]],\n\n        [[1., 1., 1., 1.],\n         [1., 1., 1., 1.],\n         [1., 1., 1., 1.]]])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones((2, 3, 4))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.826248100Z",
     "start_time": "2024-05-14T06:22:29.770025Z"
    }
   },
   "id": "3ed6360d14a50b6"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[1, 2, 3, 4],\n        [2, 3, 4, 1],\n        [3, 1, 2, 4]])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([[1, 2, 3, 4], [2, 3, 4, 1], [3, 1, 2, 4]])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.827248600Z",
     "start_time": "2024-05-14T06:22:29.780350Z"
    }
   },
   "id": "94024506a51a1ba5"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([ 3.,  4.,  6., 10.]),\n tensor([-1.,  0.,  2.,  6.]),\n tensor([0.5000, 1.0000, 2.0000, 4.0000]),\n tensor([ 1.,  4., 16., 64.]))"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([1.0, 2, 4, 8])\n",
    "y = torch.tensor([2, 2, 2, 2])\n",
    "\n",
    "x + y, x - y, x / y, x ** y"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.828249Z",
     "start_time": "2024-05-14T06:22:29.788922100Z"
    }
   },
   "id": "13078a32ac1b8d5a"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.exp(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.828249Z",
     "start_time": "2024-05-14T06:22:29.799657700Z"
    }
   },
   "id": "5327a51adbe1fde8"
  },
  {
   "cell_type": "markdown",
   "source": [
    "这个cat函数中的dim 是指按照第几个维度合并"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3eb483defe121e4b"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 4])\n",
      "torch.Size([3, 4])\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [ 2.,  1.,  4.,  3.],\n",
      "        [ 1.,  2.,  3.,  4.],\n",
      "        [ 4.,  3.,  2.,  1.]])\n",
      "tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
      "        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])\n",
      "when dim equals 0, shape is torch.Size([6, 4])\n",
      "when dim equals 1, shape is torch.Size([3, 8])\n"
     ]
    }
   ],
   "source": [
    "X = torch.arange(12, dtype=torch.float32).reshape(3, 4)\n",
    "Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
    "print(X.shape)\n",
    "print(Y.shape)\n",
    "res_1 = torch.cat((X, Y), dim=0)\n",
    "res_2 = torch.cat((X, Y), dim=1)\n",
    "print(res_1)\n",
    "print(res_2)\n",
    "print(f\"when dim equals 0, shape is {res_1.shape}\")\n",
    "print(f\"when dim equals 1, shape is {res_2.shape}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.828249Z",
     "start_time": "2024-05-14T06:22:29.809102200Z"
    }
   },
   "id": "a8a861f4ecb5ce"
  },
  {
   "cell_type": "markdown",
   "source": [
    "测试一个三维的 验证我的猜想"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "dedad1d7babf1340"
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[-0.5285, -0.9941,  1.1081, -1.0547],\n         [ 0.1227,  0.7397, -0.3378, -0.4696],\n         [ 0.3367, -1.8283, -0.1946,  0.4529]],\n\n        [[-1.9208,  0.3007,  0.4465,  0.2120],\n         [ 0.5189,  0.2783,  0.0294,  1.0045],\n         [ 1.6270, -1.1016, -1.1020, -0.2494]],\n\n        [[ 6.0000,  6.0000,  6.0000,  6.0000],\n         [ 6.0000,  6.0000,  6.0000,  6.0000],\n         [ 6.0000,  6.0000,  6.0000,  6.0000]]])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# x = torch.arange(24).reshape(2, 3, 4)\n",
    "x = torch.randn((2, 3, 4))\n",
    "add_tensor = torch.tensor([[[6, 6, 6, 6], [6, 6, 6, 6], [6, 6, 6, 6]]])\n",
    "torch.cat((x, add_tensor))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.829249200Z",
     "start_time": "2024-05-14T06:22:29.817235800Z"
    }
   },
   "id": "bd1690547033da78"
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[False,  True, False,  True],\n        [False, False, False, False],\n        [False, False, False, False]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X == Y"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:22:29.835488600Z",
     "start_time": "2024-05-14T06:22:29.827248600Z"
    }
   },
   "id": "e151db005d060d47"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(66.)"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:23:00.253905400Z",
     "start_time": "2024-05-14T06:23:00.244500900Z"
    }
   },
   "id": "6c7029c254acd51f"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[0],\n         [1],\n         [2]]),\n tensor([[0, 1]]))"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(3).reshape(3, 1)\n",
    "b = torch.arange(2).reshape(1, 2)\n",
    "a, b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:26:10.278769200Z",
     "start_time": "2024-05-14T06:26:10.271474800Z"
    }
   },
   "id": "aaa075a2d098a256"
  },
  {
   "cell_type": "markdown",
   "source": [
    "广播机制 同纬度张量进行扩充"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "53fa49a0411779d0"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0, 1],\n        [1, 2],\n        [2, 3]])"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:26:30.415113200Z",
     "start_time": "2024-05-14T06:26:30.406892700Z"
    }
   },
   "id": "f26b22874967311b"
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以用[-1] 选择最后一个元素， 可以用[1:3] 选择第一个和第二个元素\n",
    "提供了简略写法和规范写法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1ca1a5898811e7a8"
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 8.,  9., 10., 11.])\n",
      "tensor([ 8.,  9., 10., 11.])\n",
      "tensor([[ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n",
      "tensor([[ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n"
     ]
    }
   ],
   "source": [
    "print(X[-1])\n",
    "print(X[-1, :])\n",
    "print(X[1:3])\n",
    "print(X[1:3, :])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:31:02.643957900Z",
     "start_time": "2024-05-14T06:31:02.627425500Z"
    }
   },
   "id": "22349ab0c3c2e035"
  },
  {
   "cell_type": "markdown",
   "source": [
    "赋值操作 单个元素赋值"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8104604e0b17143f"
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.,  1.,  2.,  3.],\n        [ 4.,  5.,  9.,  7.],\n        [ 8.,  9., 10., 11.]])"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[1, 2] = 9\n",
    "X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:32:32.003400900Z",
     "start_time": "2024-05-14T06:32:31.992111Z"
    }
   },
   "id": "593fcaee90795e37"
  },
  {
   "cell_type": "markdown",
   "source": [
    "赋值操作 多个元素赋值"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4340acce2dd6ac4b"
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[12., 12., 12., 12.],\n        [12., 12., 12., 12.],\n        [ 8.,  9., 10., 11.]])"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0:2, :] = 12\n",
    "X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:38:00.157320300Z",
     "start_time": "2024-05-14T06:38:00.150869700Z"
    }
   },
   "id": "702e4ffaf918f514"
  },
  {
   "cell_type": "markdown",
   "source": [
    "操作 使得变量分配新的内存"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "44eb980ebd0ede46"
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(Y)\n",
    "Y = Y + X\n",
    "id(Y) == before"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:42:18.837032Z",
     "start_time": "2024-05-14T06:42:18.830018200Z"
    }
   },
   "id": "6a415ff8f2b5b20c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "执行原地操作 不分配新内存"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "554fd6bfd1be6593"
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(Z): 1648795492400\n",
      "id(Z): 1648795492400\n",
      "id(Z): 1648795916176\n"
     ]
    }
   ],
   "source": [
    "Z = torch.zeros_like(Y)\n",
    "print(f\"id(Z): {id(Z)}\")\n",
    "Z[:] = X + Y\n",
    "print(f\"id(Z): {id(Z)}\")\n",
    "# 下面还是会重新分配内存\n",
    "Z = X + Y\n",
    "print(f\"id(Z): {id(Z)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:45:18.836015300Z",
     "start_time": "2024-05-14T06:45:18.823314Z"
    }
   },
   "id": "6df139a521cf511e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "可以用 += 操作防止分配新内存"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "711573caf2a4a3de"
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(X)\n",
    "X += Y\n",
    "id(X) == before"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:46:42.583697600Z",
     "start_time": "2024-05-14T06:46:42.575665300Z"
    }
   },
   "id": "c1aa688c74dc515a"
  },
  {
   "cell_type": "markdown",
   "source": [
    "张量 与 numpy 的转换"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ba5442707fa2eb1a"
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "(numpy.ndarray, torch.Tensor)"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = X.numpy()\n",
    "B = torch.tensor(A)\n",
    "type(A), type(B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:49:03.766143200Z",
     "start_time": "2024-05-14T06:49:03.758087200Z"
    }
   },
   "id": "153505ab0c82809d"
  },
  {
   "cell_type": "markdown",
   "source": [
    "将大小为1 的张量转换为 Python 标量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4142194f3b4dbc2f"
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([3.5000]), 3.5, 3.5, 3)"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.tensor([3.5])\n",
    "a, a.item(), float(a), int(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T06:50:01.338754900Z",
     "start_time": "2024-05-14T06:50:01.330400300Z"
    }
   },
   "id": "8439494fe59198fc"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
